﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class PlanAuthVirtualFlt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("NC1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
            }
            lblError.Text = "";
            btnSwithStatus.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void btnSwithStatus_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NC2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
            {
                lblError.Text = "Please select the flight you wanted.";
                return;
            }
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_plan_approve_reject_virtual_flt";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", strArray[0]);
                para = cmd.Parameters.AddWithValue("@flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@plan_id", strArray[2]);
                para = cmd.Parameters.AddWithValue("@market_airline", Session["AIRLINE"].ToString());
                //para = cmd.Parameters.AddWithValue("@market_flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Updating failed!";
                else if (ret_code == 2)
                    lblError.Text = "Plan summitted!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + " !";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}